#!/bin/python

import serial



char2b = {"c":b'\x01',"x":b'\x09'}
b2char = {b'\x01':"雌",b'\x09':"雄"}

send_data = b''


try:
    ser = serial.Serial(port="/dev/ttyAMA0",baudrate=9600)
    print("串口打开成功")
except serial.SerialException as e:
    print(f"串口打开失败: {e}")




while 1:
    x_1 = b''
    x_2 = b''
    x_3 = b''
    send_data = b''
    data = ser.read(1)
    if data == b'\x01':
        print("A区左")
        zhonglei = input("c:雌，x:雄（,分隔）:").split(",")
        send_data = char2b[zhonglei[0]] + char2b[zhonglei[1]] + char2b[zhonglei[2]] + b'123'  # ID定死为字符串'1','2','3'
    elif data == b'\x02':
        print("A区右") 
        zhonglei = input("c:雌，x:雄（,分隔）:").split(",")
        send_data = char2b[zhonglei[0]] + char2b[zhonglei[1]] + char2b[zhonglei[2]] + b'123'  # ID定死为字符串'1','2','3'
    elif data == b'\x03':
        print('B区')
        zhonglei = input("c:雌，x:雄（,分隔）:").split(",")
        send_data = char2b[zhonglei[0]] + char2b[zhonglei[1]] + char2b[zhonglei[2]] + b'456'  # ID定死为字符串'4','5','6'
    elif data == b'\x04':
        print('C区')
        zhonglei = input("c:雌，x:雄（,分隔）:").split(",")
        send_data = char2b[zhonglei[0]] + char2b[zhonglei[1]] + char2b[zhonglei[2]] + b'789'  # ID定死为字符串'7','8','9'
    ser.write(send_data)
    ser.flushInput()

# 接受数据处理
# 1:A区左
# 2:A区右
# 3:B区
# 4:C区

# 发送格式: 花朵类型[3字节] + 花朵ID[3字节]


